using OhioState.AssetManaged;
using OhioState.Graphics.Proxy;
using Tao.OpenGl;

namespace OhioState.Graphics.OpenGL
{
    public class TransferFuncHandler : TextureHandler
    {
        public TransferFuncHandler( IAssetManaged proxyObject )
            : base(proxyObject)
        {
            TransferFunction transferFunc = _textureProxy as TransferFunction;
            transferFunc.AddChangingEventHandler(this.invalidateState);
        }
        protected override void createGLTexture()
        {
            _realTexture = new Texture1DGL(_textureProxy.Width, _textureProxy.TexelType);
        }
        protected override void updateGLTexture()
        {
            TransferFunction transferFunc = _textureProxy as TransferFunction;
            if (transferFunc != null)
            {
                int xDim;
                byte[] byte_buffer;
                int format = TextureManager.PixelFormat[transferFunc.TexelType.Format];
                int type = TextureManager.PixelBuffer[transferFunc.TexelType.Type];
                byte_buffer = transferFunc.GetTextureByte(out xDim);
                int size = xDim / transferFunc.TexelType.NumChannels;
                Gl.glTexSubImage1D(Gl.GL_TEXTURE_1D, 0, 0, size, format, type, byte_buffer);
            }
        }
    }
}
